<!-- 图片上传 -->
<template>
	<view class="repayment_proof">
		<!-- <view v-if="proofTips!= ''" class="proof_tips" :style="{color:proofTipsColor}">
			{{ proofTips }}
		</view> -->
		<view class="proof_list">
			<u-upload 
			:sizeType="['compressed']"
			:previewFullImage="true" :fileList="fileList1" 
			@afterRead="afterRead"
			@beforeRead="beforeRead"
			@delete="deletePic" name="1"
				multiple :maxCount="3"></u-upload>
		</view>
	</view>
</template>

<script>
	import App from '../../App.vue'
	export default {
		name: "UploadImg",
		data() {
			return {
				fileList1: [],
				img_url:[]
			};
		},
		props:{
			proofTipsColor:{
				type: String,
				default: '#ff9946'
			},
			proofTips:{
				type: String,
				default: '上传收款凭证（最多3张）'
			},
		},
		watch:{
			fileList1:{
				handler(val){
					console.log(this);
					// this.$emit('imgUrl',val);
				},
				deep: true
			},
			img_url:{
				handler(val){
					this.$emit('imgUrl',this.img_url)
				},
				deep: true
			}
		},
		updated() {
		},
		methods: {
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
				this.img_url.splice(event.index, 1)
			},
			//读取之前
			beforeRead(event){
				console.log('event',event)
			},
			// 新增图片读取后
			async afterRead(event) {
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					console.log('item',item);
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					console.log('result',result)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						result: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					console.log('url',url)
					// if(process.env.NODE_ENV === 'development'){
					//     // var all_url = url;
					//     var all_url = '';
					// }else{
					// 	var all_url = App.globalData.publicUrl;
					// }
					var all_url = App.globalData.publicUrl;
					let a = uni.uploadFile({
						url: all_url+'/api/resources/Upload',
						header: {
							'token': uni.getStorageSync('token'),
						},
						filePath: url,
						name: 'file',
						success: (res) => {
							console.log('upload',res)
							res = JSON.parse(res.data)
							this.img_url.push(res.url);
							this.fileList1.forEach(val=>{
								// val.url = App.globalData.publicUrl +'/'+ val.url
								val.urls = res.url;
							})
							console.log('this.fileList1',this.fileList1)
							setTimeout(() => {
								resolve(res.message)
							}, 1000)
						}
					});
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.repayment_proof {
		margin-top: 25rpx;

		.proof_tips {
			font-size: 24rpx;
			margin-bottom: 25rpx;
		}

		.proof_list {}
	}
</style>
